# -*- coding:utf-8 -*-
from common.my_time import MyTime
import re
import threading
import hashlib
import time
import os
import shutil
from base import Base
from common.custom_task_data import CustomTaskData


'''
定制任务处理
'''


class CustomTask(Base):
    def __init__(self, log_file):
        self.custom_task = CustomTaskData()
        self.url = ""
        self.file_handler = ""
        self.news = []
        self.record_title = False
        
    def __del__(self):
        pass

    def write_file_head(self, task_date, entity_name, start_date, custom_word):
        keyword = "say by data," + entity_name + "," + custom_word
        title = entity_name + "资讯"
        self.file_handler.write("<?php\n")
        self.file_handler.write("require_once rtrim($_SERVER['DOCUMENT_ROOT'],'/').'/site/config.php';\n")
        self.file_handler.write("include_once GLOBAL_PHP.'view.php';\n")
        self.file_handler.write("DisplayHead(array('keyword' => '" + keyword + "'));\n")
        self.file_handler.write("?>\n")
        self.file_handler.write("<div class=\"div_main_frame\" style=\"margin:0px;padding:12px;width:95%\">")
        self.file_handler.write("<div class=\"div_link_rank_title\" style=\"line-height:46px;font-size:30px;text-align:center;\">" + title + "</div>\n")
        self.file_handler.write("<div class=\"div_link_rank_content\" style=\"text-align:center;\">关键词:" + custom_word + "</div>\n")
        self.file_handler.write("<div class=\"div_link_rank_content\" style=\"text-align:center;\">时间:" + str(start_date) + "~" + str(task_date) + "</div>\n")

    def write_file_tail(self):
        self.file_handler.write("<div style=\"clear:both\"></div></div>\n")
        self.file_handler.write("<?php\n")
        self.file_handler.write("DisplayTail();\n")
        self.file_handler.write("?>\n")

    def recode_data(self, data_date, data_title, data_url, data_site):
        if data_title in self.news:
            return;
        '''
        if self.record_title == False:
            self.record_set_name(s)
            self.record_title = True
        '''

        self.file_handler.write("<div class=\"div_link_rank_content\" style=\"margin:0px;padding:0px;\"><a href=\"" + data_url + "\">[" + data_site + "]["+ data_date + "]" + data_title + "</a></div>\n")
        self.news.append(data_title)

    def record_set_name(self, set_name):
        self.file_handler.write("<div class=\"div_link_rank_title\" >" + set_name + "</div>\n")
        self.news = []

    def generate_file(self, task_date, entity_name, start_date, custom_word):
        temp = str(task_date) + str(start_date) + entity_name + custom_word
        self.set_end_date(str(start_date))
        self.set_max_page(20)
        self.set_check_title(False)
        file_name = hashlib.md5(temp.encode(encoding="UTF-8")).hexdigest() + ".php"
        self.file_handler = open(file_name, 'w', encoding = 'utf-8')
        self.write_file_head(task_date, entity_name, start_date, custom_word)
        self.url = str(task_date) + "/" + file_name
        entitys = self.custom_task.query_entity_info(entity_name)
        for entity in entitys:
            sets = entity[2].split("|")
            for s in sets:
                if s == "":
                    continue
                #self.record_set_name(s)
                #self.record_title = False
                search_word = []
                result_check = [[s]]
                if entity[0] != "":
                    search_word.append(entity[0])
                search_word.append("\"" + s + "\"")
                if entity[1] != "":
                    search_word.append(entity[1])
                if custom_word != "":
                    custom_word_split = custom_word.split(",")
                    search_word.append(custom_word_split[0])
                    result_check.append(custom_word_split)
                #self.get_data(search_word, result_check, ["?",";","-"])
                self.get_data_dingzhi(search_word, result_check)
        self.write_file_tail()
        self.file_handler.close()
        is_exists = os.path.exists("/home/saybydata_static/" + str(task_date))
        if not is_exists:
            os.makedirs("/home/saybydata_static/" + str(task_date))
        shutil.move(file_name, "/home/saybydata_static/" + str(task_date) + "/" + file_name)

    def process(self):
        tasks = self.custom_task.query_task()
        for task in tasks:
            self.custom_task.update_task(task[0], 1, "")
            self.generate_file(task[1], task[2], task[3], task[4])
            self.custom_task.update_task(task[0], 2, self.url)

def custom_task_api():
    task = CustomTask("./out/custom_task.log")
    task.process()

if __name__ == '__main__':
    custom_task_api()


